<template>
  <div class="skills-form">
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px">
      <el-form-item label="技能类别" prop="skillCate">
        <el-radio-group v-model="ruleForm.skillCate">
          <el-radio v-for="item in skillcateList" :key="item.value" :label="item.value">
            {{item.label}}</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="技能等级" prop="skillLevel">
        <el-radio-group v-model="ruleForm.skillLevel">
          <el-radio v-for="item in skilllevelList" :key="item.value" :label="item.value">
            {{item.label}}</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="技能名称" prop="skillName">
        <el-input v-model="ruleForm.skillName"></el-input>
      </el-form-item>
      <el-form-item label="技能描述" prop="skillDesc">
        <el-input type="textarea" v-model="ruleForm.skillDesc"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">
          {{ userItem ? '编辑' : '立即创建' }}
        </el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import SkillsApi from '@/api/skills';

export default {
  name: 'SkillsForm',
  props: ['dialogVisible', 'userItem', 'skillcateList', 'skilllevelList'],
  data() {
    return {
      ruleForm: {
        skillCate: this.userItem ? this.userItem.skill_cate.value : '',
        skillLevel: this.userItem ? this.userItem.skill_level.value : '',
        skillName: this.userItem ? this.userItem.skill_name : '',
        skillDesc: this.userItem ? this.userItem.skill_desc : '',
      },
      rules: {
        skillCate: [
          { required: true, message: '请选择技能类别', trigger: 'blur' },
        ],
        skillLevel: [
          { required: true, message: '请选择技能等级', trigger: 'blur' },
        ],
        skillName: [
          { required: true, message: '请输入技能名称', trigger: 'blur' },
          {
            min: 2,
            max: 10,
            message: '长度在2到10个字符',
            trigger: 'blur',
          },
        ],
        skillDesc: [
          { required: true, message: '请输入技能描述', trigger: 'blur' },
          {
            min: 2,
            max: 50,
            message: '长度在2到50个字符',
            trigger: 'blur',
          },
        ],
      },
    };
  },
  mounted() {
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (this.userItem) {
            this.ruleForm.id = this.userItem.id;
            SkillsApi
              .putSkill(this.ruleForm)
              .then((data) => {
                if (data.data) {
                  this.$message({
                    message: data.message,
                    type: 'success',
                  });
                  this.resetForm('ruleForm');
                  this.$emit('update:dialogVisible', false);
                } else {
                  this.$message({
                    message: data.message,
                    type: 'error',
                  });
                }
              })
              .catch((err) => {
                this.$message({
                  message: err.message,
                  type: 'error',
                });
              });
          } else {
            SkillsApi.postSkill(this.ruleForm)
              .then((data) => {
                if (data.data) {
                  this.$message({
                    message: data.message,
                    type: 'success',
                  });
                  this.resetForm('ruleForm');
                  this.$emit('update:dialogVisible', false);
                } else {
                  this.$message({
                    message: data.message,
                    type: 'error',
                  });
                }
              })
              .catch((err) => {
                this.$message({
                  message: err.message,
                  type: 'error',
                });
              });
          }
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
  watch: {
    userItem() {
      if (this.userItem) {
        this.ruleForm.skillCate = this.userItem.skill_cate.value;
        this.ruleForm.skillLevel = this.userItem.skill_level.value;
        this.ruleForm.skillName = this.userItem.skill_name;
        this.ruleForm.skillDesc = this.userItem.skill_desc;
      } else {
        this.ruleForm.skillCate = '';
        this.ruleForm.skillLevel = '';
        this.ruleForm.skillName = '';
        this.ruleForm.skillDesc = '';
      }
    },
  },
};
</script>

<style lang="scss" scoped>
.skills-form {
  width: 100%;
  height: auto;
}
</style>
